package com.money.activemq;
import com.money.dao.MyOrderMapper;
import com.money.demo.utils.Contains;
import com.money.domain.Money;
import com.money.domain.MyOrder;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.jms.*;
import java.io.Serializable;

/**
 *  监听器类：
 * @author
 *
 */
@Configuration
public class ListenerNotic implements MessageListener {

    @Resource
    private MyOrderMapper myOrderMapper;
    @Override
    public void onMessage(Message message) {
        // 接收Object消息
        if (message instanceof ObjectMessage) {
            ObjectMessage objectMessage = (ObjectMessage) message;
            Serializable serializable = null;
            try {
                serializable = objectMessage.getObject();
                MyOrder order =(MyOrder)serializable;
                //查询数据库订单状态是否是未支付状态
                MyOrder myOrder = myOrderMapper.selectByPrimaryKey(order.getId());
                if(myOrder.getIsEffective() == Contains.ORDER_TYPE_LEVEL1){
                    //未支付订单发送通知用户,如果还是未支付状态则 改成取消状态
                    myOrder.setIsEffective(Contains.ORDER_TYPE_LEVEL2);
                    myOrderMapper.updateByPrimaryKeySelective(myOrder);
                    ActiveMQTopicProducer.sendMessageByTopic("D",(Serializable)myOrder,0L);
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
        if(message instanceof TextMessage){
            TextMessage textMessage = (TextMessage) message;
            try {
                System.out.println(textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
//        TextMessage textMessage = (TextMessage) message;
//        System.out.println(textMessage.toString());
    }
}

